package xyz.thoughtset.viewer.executor.blocks.executor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.ToolExecutionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import xyz.thoughtset.viewer.ai.mcp.client.factory.McpBotFactory;
import xyz.thoughtset.viewer.executor.blocks.tool.FunctionCallBlockToolCallbackProvider;
import xyz.thoughtset.viewer.modules.step.entity.BlockTypeEnum;
import xyz.thoughtset.viewer.modules.step.entity.MCPBody;
import xyz.thoughtset.viewer.modules.step.entity.block.BlockInfo;

import java.util.List;


@Slf4j
@Component
public class MCPBlockExecutor extends AIChatExecutor<MCPBody> {
    @Autowired
    private McpBotFactory botFactory;

    @Override
    BlockTypeEnum getSupportType() {
        return BlockTypeEnum.MCP;
    }

    @Override
    protected void loadToolCallbacks(BlockInfo block,MCPBody body, ChatClient.Builder builder) {
        String clientIds = body.getMcpClientIds();
        if (StringUtils.hasText(clientIds)){
            builder.defaultToolCallbacks(
                    botFactory.clientsTools(
                            List.of(clientIds.split(","))
                    )
            );
        }
    }

}
